package com.autonavi.indoor.demo.online;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

import locating.indoor.autonavi.com.onlinelocationdemo.R;

/**
 * @author neo.duan
 * @date 2019-06-24 09:48
 * @desc 请输入文件描述
 */
public class AirActivity extends Activity {

    TextView mTvValue;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_air);

        mTvValue = (TextView) findViewById(R.id.tv_value);

        findViewById(R.id.btnGps).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gps();
            }
        });
        findViewById(R.id.btnAir).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                air();
            }
        });
    }

    private LocationManager lm;
    private void gps() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "请开启GPS权限", Toast.LENGTH_SHORT).show();
            return;
        }
        lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "开启GPS，定位更准确;取消则使用网络定位,粗略定位", Toast.LENGTH_SHORT).show();
        }

        if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            this.startActivity(intent);
        }

        lm.addGpsStatusListener(new GpsStatus.Listener() {
            @Override
            public void onGpsStatusChanged(int event) {

            }
        });

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                mTvValue.setText("海拔：" + location.getAltitude());
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        });
    }

    private SensorManager sensorManager = null;
    Sensor mPressure;
    Sensor mAccelerate;

    /**
     * 气压传感器
     */
    private void air() {
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mPressure = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
        if (mPressure == null) {
            Toast.makeText(this, "您的手机不支持气压传感器，无法使用本软件功能.", Toast.LENGTH_SHORT).show();
            return;
        }

        mAccelerate = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(pressureListener, mPressure, SensorManager.SENSOR_DELAY_NORMAL);
    }

    SensorEventListener pressureListener = new SensorEventListener() {

        @Override

        public void onSensorChanged(SensorEvent event) {
            float sPV = event.values[0];

            DecimalFormat df = new DecimalFormat("0.00");

            df.getRoundingMode();

            // 计算海拔
            double height = 44330000 * (1 - (Math.pow((Double.parseDouble(df.format(sPV)) / 1013.25),
                    (float) 1.0 / 5255.0)));

            mTvValue.setText("气压：" + sPV + "   海拔:"+ df.format(height) + "米");
        }

        @Override

        public void onAccuracyChanged(Sensor sensor, int accuracy) {


        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pressureListener != null) {
            sensorManager.unregisterListener(pressureListener);
        }
    }
}
